package ui_logic;

import connect.Client;
import game_logic.Controller;
import game_logic.GameFactory;
import ui_logic.logic.Receive;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LinkUI extends Thread {
    private JPanel panel1;
    private JButton linkRequest;
    private JTextField IP;

    private final Client client;

    public LinkUI() {
        JFrame frame = new JFrame("进行局域网连接");
        //设置窗口大小
        frame.setSize(350, 200);
        frame.setContentPane(panel1);
        //设置按下右上角X号后关闭
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        //窗口居中
        frame.setLocationRelativeTo(null);
        //窗口可见
        frame.setVisible(true);
        try {
            client = new Client();
//            client.start();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        IP.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                String checkIP = IP.getText();
                if (checkIP.length() >= 15) e.consume();
            }
        });
        linkRequest.setText("请求与对方联机");
    }

    //TODO 连接中弹窗，希望在收到信息后就自动关闭，以下是弹窗类
//    class LinkingDialog extends Thread {
//        // 创建一个JFrame作为父窗口
//        private JFrame parentFrame = new JFrame();
//        // 创建一个JDialog作为文本弹窗
//        private JDialog dialog = new JDialog(parentFrame, " ", true);
//
//        public LinkingDialog() {
//            String message = "请求连接中...";
//            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//            // 创建一个JLabel用于显示文本消息
//            JLabel label = new JLabel(message);
//            label.setHorizontalAlignment(JLabel.CENTER); // 设置文本居中对齐
//            // 添加文本标签到弹窗
//            dialog.getContentPane().add(label);
//            // 设置弹窗大小和位置
//            dialog.setSize(150, 100);
//            dialog.setLocationRelativeTo(null);
//        }
//
//        public void dialogIsVisible() {
//            // 显示弹窗
//            dialog.setVisible(true);
//        }
//
//        public void dialogIsInvisible() {
//            dialog.setVisible(false);
//        }
//
//        @Override
//        public void run() {
//            try {
//                while (true) {
//                    dialogIsVisible();
//                }
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }
//    }

    public void startLinking() {
        // use RE to constrain input
        String patchIp = "^((\\d{1,3})\\.){3}(\\d{1,3})$";
        linkRequest.addActionListener(e -> {
            String inputIP = IP.getText();
            if (!inputIP.matches(patchIp))
                JOptionPane.showMessageDialog(panel1, "你需要输入正确的IP地址", "Warning", JOptionPane.WARNING_MESSAGE);
            else {
//                LinkingDialog show = new LinkingDialog();
//                        show.start();
                Client.setLinkFinish();
                Client.callSendMessage(inputIP, "来下把棋");
//                            show.dialogIsInvisible();
//                       client.release();
            }
        });
    }

    @Override
    public void run() {
        Receive r = new Receive();//client.getLinkFinish() != 1
        while (r.receiveDone!=1) {
            Client.callReceiveMessage();
            r.receiveSingle(client, panel1);
        }
        System.out.println("即将进入游戏 ");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            LinkUI t = new LinkUI();
            t.startLinking();
            t.start();
        });
    }
}
